﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_Business")]
    public class T_Business :IEntity<T_Business>
    {
        [Key]
        [Column(TypeName="int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Name { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string ContactInfo { get; set; }
        public DateTime CreateDate { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string Creator { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Logo { get; set; }

        /// <summary>
        /// 主营
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string MainSell { get; set; }
        /// <summary>
        /// 送餐时间
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string DeliveryTime { get; set; }

        /// <summary>
        /// 送餐时间
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string DeliveryTimeEnd { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Address { get; set; }
        /// <summary>
        /// 特色菜
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string SpecialFood { get; set; }
        /// <summary>
        /// 等级（显示星星）
        /// </summary>
        [Column(TypeName = "int")]
        public int Levels { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string ContactPeople { get; set; }

        public override T_Business Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Business Find(System.Linq.Expressions.Expression<Func<T_Business, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Business.FirstOrDefault(where);
                if (item != null)
                {
                    this.ContactInfo = item.ContactInfo;
                    this.CreateDate = item.CreateDate;
                    this.Creator = item.Creator;
                    this.Id = item.Id;
                    this.Logo = item.Logo;
                    this.Name = item.Name;
                    this.DeliveryTime = item.DeliveryTime;
                    this.Levels = item.Levels;
                    this.MainSell = item.MainSell;
                    this.SpecialFood = item.SpecialFood;
                    this.Address = item.Address;
                    this.DeliveryTimeEnd = item.DeliveryTimeEnd;
                    this.ContactPeople = item.ContactPeople;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Business, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Business.Any(where);
            }
        }

        public override T_Business FindLast(System.Linq.Expressions.Expression<Func<T_Business, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Business.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.ContactInfo = item.ContactInfo;
                    this.CreateDate = item.CreateDate;
                    this.Creator = item.Creator;
                    this.Id = item.Id;
                    this.Logo = item.Logo;
                    this.Name = item.Name;
                    this.DeliveryTime = item.DeliveryTime;
                    this.Levels = item.Levels;
                    this.MainSell = item.MainSell;
                    this.SpecialFood = item.SpecialFood;
                    this.Address = item.Address;
                    this.DeliveryTimeEnd = item.DeliveryTimeEnd;
                    this.ContactPeople = item.ContactPeople;
                    return this;
                }
                return null;
            }
        }
    }
}
